
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;

namespace eCom
{
	public class eComTaxesBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getTaxDefinitions(IApplication app, IRuleContext context, Swaf.BGuid.Guid companyId)
		{
			context.put("companyId", companyId==null?ActiveCompanyId:companyId);
			IBizObjList taxes = app.appData.get("DA.eCom.GetCompanyTaxDefinitions", context) as IBizObjList;
			return taxes;
		}

		public void updateTaxDefinition(IApplication app, IRuleContext context, IBizObj tax)
		{
			if (tax.isNew)
			{
				tax.put("companyId", ActiveCompanyId);
				app.appData.put("DA.eCom.AddCompanyTaxDefinition", tax);
			}
			else
				app.appData.put("DA.eCom.UpdateCompanyTaxDefinition", tax);
		}

		public void deleteTaxDefinition(IApplication app, IRuleContext context, Swaf.BGuid.Guid taxId)
		{
			context.put("id", taxId);
			app.appData.put("DA.eCom.DeleteCompanyTaxDefinition", context);
		}

		public IBizObjList getProductTaxes(IApplication app, IRuleContext context, Swaf.BGuid.Guid productId)
		{
			context.put("productId", productId);
			IBizObjList taxes = app.appData.get("DA.eCom.GetProductTaxes", context) as IBizObjList;
			return taxes;
		}

		public void setProductTaxes(IApplication app, IRuleContext context, Swaf.BGuid.Guid productId, IBizObjList taxesToRemove, IBizObjList taxesToAdd)
		{
			try
			{
				if (productId != null)
				{
					context.put("productId", productId);
					if (taxesToRemove != null)
						foreach (IBizObj tax in taxesToRemove)
						{
							context.put("taxId", tax.get("id"));
							app.appData.put("DA.eCom.DeleteProductTax", context);
						}
					if (taxesToAdd != null)
						foreach (IBizObj tax in taxesToAdd)
						{
							context.put("taxId", tax.get("id"));
							app.appData.put("DA.eCom.AddProductTax", context);
						}
				}
			}
			catch (Exception e)
			{
				throw new eComException("Error executing setProductTaxes.", e);
			}
		}

		public IBizObj updateSalesOrderItemTaxes(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObj orderItem = resultValue as IBizObj;
			if (orderItem != null)
			{
				IBizObjList taxes = context.RuleGroup.execute(context, "getProductTaxes", orderItem.get("product.id")).CallResults as IBizObjList;
				if (taxes != null)
				{
					context.put("itemId", orderItem.get("id"));
					context.put("orderId", orderItem.get("orderId"));
					app.appData.put("DA.eCom.DeleteOrderItemTaxes", context);

					foreach (IBizObj tax in taxes)
					{
						Decimal taxVal = (Decimal)tax.get("baseAddOn");
						taxVal += (Decimal)orderItem.get("total") * ((Decimal)tax.get("percentCost") / 100M);
						context.put("taxId", tax.get("id"));
						context.put("total", taxVal);
						app.appData.put("DA.eCom.AddOrderItemTax", context);
					}
				}
			}
			return orderItem;
		}

		public IBizObj getSalesOrderTaxes(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObj salesOrder = resultValue as IBizObj;
			IBizObjList taxes = app.appData.get("DA.eCom.GetSalesOrderTaxes", salesOrder) as IBizObjList;
			if (taxes != null && taxes.numElements > 0)
			{
				IBizObjList orderTaxes = (IBizObjList)salesOrder.get("taxes");
				Decimal total = 0M;
				Swaf.BGuid.Guid lastTaxId = null;
				for(int p = 0; p < taxes.numElements;p++)
				{
					IBizObj tax = taxes[p];
					total += (Decimal)tax.get("total");
					
					if (!tax.get("taxId").Equals(lastTaxId) || p == taxes.numElements-1)
					{
						IBizObj orderTax = orderTaxes.insertNew();
						orderTax.put("taxId", tax.get("taxId"));
						orderTax.put("name", tax.get("name"));
						orderTax.put("total", total);
						total = 0M;
					}
					lastTaxId = (Swaf.BGuid.Guid)tax.get("taxId");
				}
			}
			return salesOrder;
		}
	}
}
